<?php
use EasyWeChat\OfficialAccount\Application;
use Predis\Client;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Lock\Store\RedisStore;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Lock;
/**
 * @name IndexController
 * @author Lenovo
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
class IndexController extends Yaf_Controller_Abstract {
    

    protected $db;

    public function init() {
        $this->db = Singleton::getDb();
    }

    public function smsAction()
    {
        sendSMS_aliyun('15852758579',['code'=>mt_rand(100000,999999)],'SMS_154950909');
    }

    public function storeAction() {
        $data = [
            'name'  => 'thinkphp',
            'age' => 200,
            'email' => 'thinkphp@qq.com',
        ];
        $validate = new UserModel;
        if (!$validate->check($data)) {
            error($validate->getError());
        }
        
    }


	public function indexAction() {
        $config = [
            'app_id' => 'wx8b1afa03aea3f17d',
            'secret' => 'c88b415bd6bc9a8b3eedaa4b6c4087cd',
            'token' => 'easywechat',
            'aes_key' => '', 
            'http' => [
                'timeout' => 5.0,
                'retry' => true, // 使用默认重试配置
            ],
        ];

        $app = new Application($config);

        $server = $app->getServer();
        $result = $server->serve();
        echo($result->getBody());
	}



    public function lockAction() {
        $store = new RedisStore(new Client([
            'scheme' => "tcp",
            'host' => "127.0.0.1",
            'port' => "6379",
            'cache' => "0",
            'password' => "2020",
        ]));
//        $factory = new LockFactory($store);
//
//        $lock = $factory->createLock('notification-flush');
//        $ret = $lock->acquire(true);

        $key = new Key('notification-flush-release');
        $lock = new Lock($key,  $store, 3, false);
        $ret = $lock->acquire(true);
        $lock->release();
        echo $ret;
    }

}
